<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Order\OrderManagement;
use App\Http\Controllers\Business\Admin\Statistic\StatisticManagement;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class StatisticsController extends WebController
{
    //统计数据
    public function statisticalSurvey() {
        $cityId = $this->request->get('cityid');
        $data = StatisticManagement::survey($cityId);
        $data['cityid'] = $cityId;
        return view('console.statistics.statistical_survey', $data);
    }
    //折线图
    public function getChartData() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('day')) {
            $days = $this->request->get('day');
        } else {
            $days = 7;
        }
        $timeAxis = StatisticManagement::getTimeAxis($cityId, $days);
        $orderData = StatisticManagement::getOrderChartData($cityId, $days);//TODO 有待优化
        //$orderData = [1,2,3,4,5,4,3];
        $userData = StatisticManagement::getUserChartData($cityId, $days);
        $data = [
            'status' => 'success',
            'data' => [
                'timeaxis' => $timeAxis,
                'order' => $orderData,
                'user' => $userData
            ]
        ];
        return json_encode($data);
    }
    //通过品类去获取订单数据
    public function orderListByClassId() {
        $cityId = $this->request->get('cityid');
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $result = StatisticManagement::orderListByClassId($cityId, $startTime, $lastTime);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        dd($data);return;
    }
    //导出通过品类去获取订单数据
    public function exportOrderListByClassId() {

    }
    //通过商家获取订单数据
    public function orderListByShop() {
        $perpage = 50; //默认显示50条
        $cityId = $this->request->get('cityid');
        if($this->request->has('search')) {
            $search = $this->request->get('search');
            $skip = 0;
            $take = $perpage;
            $page=1;
        } else {
            if ($this->request->has('page')) {
                $page = $this->request->get('page');
            } else {
                $page = 1;
            }
            $skip = ($page-1)*$perpage;
            $take = $perpage;
            $search = '';
        }
        if($this->request->has('starttime')) {
            $startTime = $this->request->get('starttime');
            $lastTime = $this->request->get('lasttime');
            $startTime = date('Y-m-d',strtotime($startTime)) . " 00:00:00";
            $lastTime = date('Y-m-d',strtotime($lastTime)) . " 23:59:59";
        } else {
            if($this->request->has('type')) {
                $type = $this->request->get('type');
            } else {
                $type = 'thismonth';
            }
            if($type == 'today') {
                $startTime = date('Y-m-d') . ' 00:00:00';
                $lastTime = date('Y-m-d') . ' 23:59:59';;
            } elseif($type == 'yesterday') {
                $startTime = date('Y-m-d', strtotime("-1day")) . ' 00:00:00';
                $lastTime = date('Y-m-d', strtotime("-1day")) . ' 23:59:59';
            } elseif($type == 'week') {
                $startTime = date("Y-m-d",strtotime("-1 week Monday")) . " 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'thismonth') {
                $startTime = date('Y-m') . "-01 00:00:00";
                $lastTime = date('Y-m-d') . " 23:59:59";
            } elseif($type == 'lastmonth') {
                $firstDay = date("Y-m-01");
                $startTime = date('Y-m-01', strtotime($firstDay)-86400) . " 00:00:00";
                $lastTime = date("Y-m-d", strtotime($firstDay)-86400) . " 23:59:59";
            } elseif($type == 'all') {
                $startTime = "2016-07-01 00:00:00";
                $lastTime = date("Y-m-d") . " 23:59:59";
            } else {
                return view('errors.404');
            }
        }
        $count = StatisticManagement::getOrderCountByFromDateAndToDate($cityId, $startTime, $lastTime);
        $lastPage = ceil($count/$perpage);
        $result = StatisticManagement::orderListByShop($cityId, $startTime, $lastTime,50, $page, $search);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'cityid' => $cityId,
            'starttime' => $startTime,
            'lasttime' => $lastTime,
            'lists' => $lists,
            'search' => $search,
            'page' => $page,
            'lastpage' => $lastPage,
            'perpage' => $perpage,
        ];
        return view('console.statistics.order_list_by_shop', $data);
    }
    //导出通过商家获取订单数据
    public function exportOrderListByShop() {
        $startTime = $this->request->get('starttime');
        $startTime = $startTime . " 00:00:00";
        $lastTime = $this->request->get('lasttime');
        $lastTime = $lastTime . " 23:59:59";
        if($this->request->has('search')) {
            $search = $this->request->get('search');
        } else {
            $search = '';
        }
        $cityId = $this->request->get('cityid');
        $result = StatisticManagement::exportOrderListByShop($cityId,$startTime, $lastTime, $search);
        if($result['status'] == 'failed') {
            return $this->responseError('导出失败');
        }

        $cellData = [
            ['商家代码', '商家名称', '商家最新状态','区县','商圈','商务人员','消费金额','消费实付','消费订单数']
        ];
        $data = $result['data'];
        foreach($data as $list) {
            $newList = [
                'shopcode' => $list['shopcode'],
                'shopname' => $list['shopname'],
                'status' => $list['status'] ==1?'在线':'下架',
                'districtname' => $list['districtname'],
                'businessareaname' => $list['businessareaname'],
                'realname' => $list['realname'],
                'sumorderamount' => $list['sumorderamount'],
                'sumactualamount' => $list['sumactualamount'],
                'ordercount' => $list['ordercount'],
            ];
            array_push($cellData, $newList);
        }
        $title = '商家订单统计列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('商家订单统计列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
}